package com.wmx.wmxredis.controller;

import com.wmx.wmxredis.beans.Person;
import com.wmx.wmxredis.resultAPI.ResultData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 借助 {@link RedisCacheUtil} 工具类 操作 Redis
 *
 * @author wangMaoXiong
 */
@RestController
public class RedisCacheController {

    private static final String cacheKeyPrefix = "person#";

    @Resource
    private RedisCacheUtil redisCacheUtil;

    /**
     * 设置缓存：http://localhost:8080/redis2/save?id=1000&name=张三
     *
     * @return
     */
    @GetMapping("redis2/save")
    public ResultData<Object> save(@RequestParam Integer id,
                                   @RequestParam String name) {
        Person person = new Person();
        person.setId(id);
        person.setName(name);
        person.setBirthday(new Date());

        redisCacheUtil.setCacheObject(cacheKeyPrefix + "id#" + id, person);
        return ResultData.success("缓存成功");
    }

    /**
     * 设置缓存：http://localhost:8080/redis2/saveList?menuId=uuu100
     *
     * @return
     */
    @GetMapping("redis2/saveList")
    public ResultData<Object> saveList(@RequestParam String menuId) {
        String cacheKey = cacheKeyPrefix + "menuId#" + menuId;
        int size = 10;
        List<Person> persons = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Person person = new Person();
            person.setId(1);
            person.setName("张" + i + "丰");
            person.setBirthday(new Date());
            persons.add(person);
        }
        redisCacheUtil.setCacheObject(cacheKey, persons);
        return ResultData.success("缓存成功");
    }

    /**
     * 查询缓存：http://localhost:8080/redis2/get?id=1000
     *
     * @param id
     * @return
     */
    @GetMapping("redis2/get")
    public ResultData getRedisCache(@RequestParam Integer id) {
        Person person = redisCacheUtil.getCacheObject(cacheKeyPrefix + "id#" + id);
        return ResultData.success(person);
    }

    /**
     * 查询缓存：http://localhost:8080/redis2/get2?menuId=uuu100
     *
     * @param menuId
     * @return
     */
    @GetMapping("redis2/get2")
    public ResultData getRedisCache2(@RequestParam String menuId) {
        String cacheKey = cacheKeyPrefix + "menuId#" + menuId;
        List<Person> persons = redisCacheUtil.getCacheObject(cacheKey);
        return ResultData.success(persons);
    }

    /**
     * 模糊删除key
     * http://localhost:8080/redis2/delKeyPatternV1?pattern=*1*
     *
     * @param pattern ：key 匹配模式，比如 basic*，模糊匹配'basic'字开头的key；*basic* 模糊匹配保护'basic'的key
     * @return
     */
    @GetMapping("redis2/delKeyPatternV1")
    public ResultData delKeyPatternV1(@RequestParam String pattern) {
        long l = redisCacheUtil.delKeyPatternV1(pattern);
        return ResultData.success(l);
    }

    /**
     * 模糊删除key
     * http://localhost:8080/redis2/delKeyPattern2?pattern=*1*
     *
     * @param pattern ：key 匹配模式，比如 basic*，模糊匹配'basic'字开头的key；*basic* 模糊匹配保护'basic'的key
     * @return
     */
    @GetMapping("redis2/delKeyPatternV2")
    public ResultData delKeyPatternV2(@RequestParam String pattern) {
        long l = redisCacheUtil.delKeyPatternV2(pattern);
        return ResultData.success(l);
    }

    /**
     * Redis 集群部署时的模糊查询 http://localhost:8080/redis2/getKeysByPatternForCluster?pattern=*b*
     *
     * @param pattern ：模糊查询内容，比如 *BASIC_SERVER*
     * @return ：返回模糊查询的key集合
     */
    @GetMapping("redis2/getKeysByPatternForCluster")
    public ResultData<Object> getKeysByPatternForCluster(@RequestParam String pattern) {
        Set<String> keys = redisCacheUtil.scanByPatternForCluster(pattern);
        return ResultData.success(keys);
    }

}

